/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DriverNoticia;

import java.util.regex.*;
import java.util.StringTokenizer;

/**
 *
 * @author Juan Manuel Fuentes Diaz
 */
public class ParserHttp {

    private String titulo;
    private String texto;

    public void crearNoticia() {
        this.titulo = "";
        this.texto = "";
    }

    private boolean contieneTitulo(StringBuffer str) {
        return ((str.lastIndexOf("<title>") != -1) && (str.lastIndexOf("</title>") != -1));
    }

    private boolean contieneTextoP(StringBuffer str) {
        boolean p = ((str.lastIndexOf("<p>") != -1) && (str.lastIndexOf("</p>") != -1));
        return p;
    }

    private boolean contieneTextoH1(StringBuffer str) {
        boolean p = ((str.lastIndexOf("<h1>") != -1) && (str.lastIndexOf("</h1>") != -1));
        return p;
    }

    private boolean contieneTextoH2(StringBuffer str) {
        boolean p = ((str.lastIndexOf("<h2>") != -1) && (str.lastIndexOf("</h2>") != -1));
        return p;
    }

    private boolean contieneBasuraFinal(StringBuffer str) {
        boolean p = ((str.lastIndexOf("<") != -1) && (str.lastIndexOf(">") != -1));
        return p;
    }

    private boolean contieneBasuraScript(StringBuffer str) {
        boolean p = ((str.lastIndexOf("<script") != -1) && (str.lastIndexOf("</script>") != -1));
        return p;
    }

    private boolean contieneBasuraA(StringBuffer str) {
        boolean p = ((str.lastIndexOf("<a") != -1) && (str.lastIndexOf("</a>") != -1));
        return p;
    }

    private boolean contieneBasuraLabel(StringBuffer str) {
        boolean p = ((str.lastIndexOf("<label") != -1) && (str.lastIndexOf("</label>") != -1));
        return p;
    }

    private boolean contieneBasuraDiv(StringBuffer str) {
        boolean p = ((str.lastIndexOf("<div") != -1) && (str.lastIndexOf("</div>") != -1));
        return p;
    }

    private String tokenizarPunt(String st) {
        StringTokenizer stok = new StringTokenizer(st, ".");
        int i;
        String token;
        StringBuffer list = new StringBuffer("");
        while (stok.hasMoreTokens()) {
            token = stok.nextToken();
            list.append(token);
            list.append(". ");
        }
        return list.toString();
    }
    private String tokenizarPuntSep(String st) {
        StringTokenizer stok = new StringTokenizer(st);
        String ac = "acute;";
        String ac2 = "quot";
        CharSequence ch = ac.subSequence(0, ac.length()-1);
        CharSequence ch2 = ac2.subSequence(0, ac.length()-1);
        String token;
        StringBuffer list = new StringBuffer("");
        while (stok.hasMoreTokens()) {
            token = stok.nextToken();
            if (token.compareTo(".") == 0 || token.compareTo("nbsp;") == 0 || token.compareTo("nbsp") == 0 || 
                    token.contains(ch) || token.contains(ch2)){
                token = "";
            }
            list.append(token);
            list.append(" ");
        }
        return list.toString();
    }
    private String tokenizarSig(String st) {
        char[] punt={':',';','!','\\',
        '|','$','#','%','&','/','(',')','=','-','_','[',']',
        '@','1','2','3','4','5','6','7','8','9','0',169};
        StringTokenizer stok = new StringTokenizer(st);
        int i;
        String token;
        StringBuffer list = new StringBuffer("");
        while (stok.hasMoreTokens()) {
            token =stok .nextToken();
            for(i=0;i<token.length();i++){
                for(int j=0;j<punt.length;j++){
                    if (token.charAt(i)==punt[j]){
                        token = token.replace(token.charAt(i),' ');
                    }
                }
            }
            list.append(token);
            list.append(" ");
        }
        return list.toString();
    }

    public boolean esNoticia(StringBuffer str) {
        return contieneTitulo(str) || contieneTextoP(str);
    }

    public TitTex sacaTexto(StringBuffer str) {
        StringBuffer titbs = new StringBuffer();
        StringBuffer tit = new StringBuffer();
        StringBuffer aux = new StringBuffer(str);
        String st = "";
        String stri;
        if (this.contieneTitulo(str)) {
            titbs.append(str.substring(str.lastIndexOf("<title>") + 7, str.lastIndexOf("</title>")));
            titbs.append(". ");
            tit.append(str.substring(str.lastIndexOf("<title>") + 7, str.lastIndexOf("</title>")));
            }
        if (this.contieneTextoP(str)) {
            while (this.contieneTextoP(aux)) {
                titbs.append(aux.substring(aux.lastIndexOf("<p>") + 3, aux.lastIndexOf("</p>")));
                titbs.append(".");
                aux = new StringBuffer(aux.replace(aux.lastIndexOf("<p>"), aux.lastIndexOf("</p>") + 4, "."));
            }
        }
        if (this.contieneTextoH1(str)) {
            while (this.contieneTextoH1(aux)) {
                titbs.append(aux.substring(aux.lastIndexOf("<h1>") + 4, aux.lastIndexOf("</h1>")));
                titbs.append(".");
                aux = new StringBuffer(aux.replace(aux.lastIndexOf("<h1>"), aux.lastIndexOf("</h1>") + 5, "."));
            }
        }
        if (this.contieneTextoH2(str)) {
            while (this.contieneTextoH2(aux)) {
                titbs.append(aux.substring(aux.lastIndexOf("<h2>") + 4, aux.lastIndexOf("</h2>")));
                titbs.append(".");
                aux = new StringBuffer(aux.replace(aux.lastIndexOf("<h2>"), aux.lastIndexOf("</h2>") + 5, "."));
            }
        }
        if (this.contieneBasuraScript(titbs)) {
            while (this.contieneBasuraScript(titbs)) {
                titbs = new StringBuffer(titbs.replace(titbs.lastIndexOf("<script"), titbs.lastIndexOf("</script>") + 9, " "));
            }
        }
        if (this.contieneBasuraA(titbs)) {
            while (this.contieneBasuraA(titbs)) {
                titbs = new StringBuffer(titbs.replace(titbs.lastIndexOf("<a"), titbs.lastIndexOf("</a") + 4, " "));
            }
        }
        if (this.contieneBasuraLabel(titbs)) {
            while (this.contieneBasuraLabel(titbs)) {
                titbs = new StringBuffer(titbs.replace(titbs.lastIndexOf("<label"), titbs.lastIndexOf("</label>") + 8, " "));
            }
        }
        if (this.contieneBasuraDiv(titbs)) {
            while (this.contieneBasuraDiv(titbs)) {
                if (titbs.lastIndexOf("<div") < titbs.lastIndexOf("</div>")) {
                    titbs = new StringBuffer(titbs.replace(titbs.lastIndexOf("<div"), titbs.lastIndexOf("</div>") + 6, " "));
                } else {
                    titbs = new StringBuffer(titbs.replace(titbs.lastIndexOf("<div"), titbs.lastIndexOf("<div") + 4, " "));
                }
            }
        }
        if (this.contieneBasuraFinal(titbs)) {
            while (this.contieneBasuraFinal(titbs)) {
                if (titbs.lastIndexOf("<") < titbs.lastIndexOf(">")) {
                    titbs = new StringBuffer(titbs.replace(titbs.lastIndexOf("<"), titbs.lastIndexOf(">") + 1, " "));
                } else {
                    titbs = new StringBuffer(titbs.replace(titbs.lastIndexOf("<"), titbs.lastIndexOf("<") + 1, " "));
                }
            }
        }

        stri = titbs.toString();
        st = "";
        if (stri.indexOf('\t') != -1) {
            for (int x = 0; x < stri.length(); x++) {
                if (stri.charAt(x) != '\t') {
                    st += stri.charAt(x);
                }
            }
        } else {
            st = new String(stri);
        }
        stri = new String(st);
        st = "";
        for (int x = 0; x < stri.length(); x++) {
            if (stri.charAt(x) != ' ') {
                st += stri.charAt(x);
            } else {
                x++;
                if (x < stri.length()) {
                    if (stri.charAt(x) != ' ') {
                        st += ' ';
                        st += stri.charAt(x);
                    }
                }
            }
        }
        st = new String(tokenizarPunt(st));
        st = new String(tokenizarSig(st));
        st = new String(tokenizarPuntSep(st));
        TitTex tt = new TitTex();
        tt.titulo = tit.toString();
        tt.texto = st;
        return tt;
    }
}
